
package pack;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import net.useobjects.World;
import net.useobjects.geom.Position;

public class Controller {
    
    private final double amplitude;
    private final double deltaAngle;
    private double x,y;
    private double orientation;
    private final World svet;
    private Position position;
    private ArrayList<Kamen> zoznamKamenov;
    private ArrayList<Strela> zoznamStriel;
    private Strela strela;
    private int deltaL=10; //o kolko sa pohne dopredu
    
    public Controller(World svet, ArrayList zoznamKamenov,ArrayList zoznamStriel) {
        
        this.svet = svet;
        this.zoznamKamenov = zoznamKamenov;
        this.zoznamStriel = zoznamStriel;
        amplitude = 10;
        deltaAngle = 0.1;
        position = new Position(0,0);
    }
    
    void performAction(Strelec strelec) {
               
            orientation = strelec.getRotation()-(Math.PI/2.0);
//            x = amplitude*Math.sin(strelec.getRotation());
//            y = amplitude*Math.cos(strelec.getRotation());
//            x = amplitude*Math.sin(orientation);
//            y = amplitude*Math.cos(orientation);
            position=strelec.getPositionInRootGroup();

            
            if(svet.isKeyPressed(KeyEvent.VK_UP)) {
                
//                x *= +1;
//                y *= -1;
                
                if(strelec.jeVoSvete(position,orientation, KeyEvent.VK_UP)) {// && strelec.mozePremiestnit(position,orientation,kamen)) {
                    
                    if (strelec.mozemSaPohnut(strelec,zoznamKamenov,KeyEvent.VK_UP)) {
//                           strelec.move(x, y);
                           strelec.moveInDirection(deltaL, orientation);
                    }
                }
            }
            
            
            if(svet.isKeyPressed(KeyEvent.VK_DOWN)) {
                
//                x *= -1;
//                y *= +1;
                
                if(strelec.jeVoSvete(position,orientation, KeyEvent.VK_DOWN)) {
                    
                    if (strelec.mozemSaPohnut(strelec,zoznamKamenov,KeyEvent.VK_DOWN)) {
                           //strelec.move(x, y);
                        strelec.moveInDirection(deltaL, orientation+Math.PI);
                    }
                }
            }
            
            
            if(svet.isKeyPressed(KeyEvent.VK_LEFT)) {
                double delta;
                
                if(orientation > 0) {
                    delta = -deltaAngle;
                }
                
                else {
                    delta = -deltaAngle;
                }
                    
                strelec.setRotation(strelec.getRotation()+delta);
            }
         
            
            if(svet.isKeyPressed(KeyEvent.VK_RIGHT)) {
                double delta;
                
                if(orientation > 0) {
                    delta = deltaAngle;
                }
                
                else {
                    delta = +deltaAngle;
                }
                
                strelec.setRotation(strelec.getRotation()+delta);
            }
            
            //vystrel
            if(svet.isKeyPressed(KeyEvent.VK_SPACE)) {
                position = strelec.getPolygon().getPositionInRootGroup();
                
                strela = new Strela(svet.getRootGroup(),position,orientation,zoznamKamenov,zoznamStriel);
                zoznamStriel.add(strela);
            }
        
    }
    
       
    
}
